﻿using Mengsk.ProxyDelivery.Domain;
using Mengsk.ProxyDelivery.Service;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Data;

namespace Mengsk.ProxyDelivery.Converters
{
    public class SellerConverter : IValueConverter
    {
        private static Dictionary<int, string> sellerNames = new Dictionary<int, string>();

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value == null || value.GetType() != typeof(int))
            {
                return value;
            }

            int i = (int)value;

            if (i < 1)
            {
                return "";
            }

            if (sellerNames.ContainsKey(i))
            {
                return sellerNames[i];
            }
            lock (sellerNames)
            {
                SellerService service = ServiceContainer.GetService<SellerService>();
                Seller op = service.GetById(i);
                if (op == null)
                {
                    return "卖家不存在";
                }
                sellerNames[i] = op.Name;
                return op.Name;
            }
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}
